from django.db import models
import json

# Create your models here.
class User(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    score = models.CharField(max_length=20,verbose_name='积分')
    email = models.CharField(max_length=20,verbose_name='邮箱')
    phone = models.CharField(max_length=11,unique=True,verbose_name='手机号')
    password = models.CharField(max_length=32,verbose_name='密码') # md5加密
    name = models.CharField(max_length=20,verbose_name='昵称')
    image = models.CharField(max_length=100,verbose_name='头像') # 头像url
    qq = models.CharField(max_length=20,verbose_name='qq号码')
    last = models.DateTimeField(verbose_name='最后登录时间')
    
    
    class Meta:
        db_table = 'tb_user' #指明数据库表名
        verbose_name = '用户' #在admin站点中显示的名称
        verbose_name_plural = verbose_name #显示的复数名称

from frontend.models import Goods

class Comment(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    time = models.DateTimeField(verbose_name='时间') # datetime.datetime.now()
    text = models.CharField(max_length=200,verbose_name='内容')
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    # userId = models.IntegerField(verbose_name='用户id')
    goods = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品')
    # goodsId = models.IntegerField(verbose_name='商品id')
    ischecked = models.BooleanField( default=False,verbose_name='是否通过审核')
    
    class Meta:
        db_table = 'tb_comment' #指明数据库表名
        verbose_name = '评论' #在admin站点中显示的名称
        verbose_name_plural = verbose_name #显示的复数名称


class Admin(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    username = models.CharField(max_length=10,unique=True,verbose_name='用户名')
    password = models.CharField(max_length=32,verbose_name='密码')
    authority = models.IntegerField(verbose_name='权限') # 0 high,1 low
    
    class Meta:
        db_table = 'tb_admin' #指明数据库表名
        verbose_name = '管理员' #在admin站点中显示的名称
        verbose_name_plural = verbose_name #显示的复数名称

class Notify(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    goods = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品')
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    price = models.FloatField(verbose_name='当前价格')
    
    class Meta:
        db_table = 'tb_notify' #指明数据库表名
        verbose_name = '降价通知' #在admin站点中显示的名称
        verbose_name_plural = verbose_name #显示的复数名称

class DailyStatistics(models.Model):
    date = models.DateField(primary_key=True,verbose_name="日期") # datetime.date.today()
    login = models.IntegerField(default=0,verbose_name="登录数")
    search = models.IntegerField(default=0,verbose_name="搜索数")
    comment = models.IntegerField(default=0,verbose_name="评论数")
    collection = models.IntegerField(default=0,verbose_name="收藏数")
    useradd = models.IntegerField(default=0,verbose_name="新增用户数")

    class Meta:
        db_table = 'tb_dailystatistics' #指明数据库表名
        verbose_name = '每日统计' #在admin站点中显示的名称
        verbose_name_plural = verbose_name #显示的复数名称

    def __str__(self) -> str:
        return json.dumps({
            'date':str(self.date),
            'login':self.login,
            'search':self.search,
            'comment':self.comment,
            'collection':self.collection,
            'useradd':self.useradd
        })

class Message(models.Model):
    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='关联用户')
    attribute = models.CharField(max_length=5,default='in',verbose_name='邮件属性')
    classify = models.CharField(max_length=10,default='system',verbose_name='内容分类')
    title = models.CharField(max_length=30,verbose_name='标题')
    content = models.CharField(max_length=500,verbose_name='内容')

    class Meta:
        db_table = 'tb_message' #指明数据库表名
        verbose_name = '消息' #在admin站点中显示的名称
        verbose_name_plural = verbose_name #显示的复数名称
class OrderInfo(models.Model):
    """定单"""
    order_id = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="支付编号")
    status = models.CharField(max_length=10, default='UNPAID', verbose_name="状态")
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    total_amount = models.CharField(max_length=50, default='0', verbose_name="总金额") # 总金额
    class Meta:
        db_table = 'tb_orderinfo'
        verbose_name = '定单'
        verbose_name_plural = verbose_name

class Payment(models.Model):
    """支付信息"""
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name='订单')
    trade_id = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="支付编号")

    class Meta:
        db_table = 'tb_payment'
        verbose_name = '支付信息'
        verbose_name_plural = verbose_name